/**
 * @Auther: ChenforCode
 * @Date: 2018/9/12 15:45
 * @Description: 完成用户登陆的相关控制
 */
package cn.chenforcode.controller;

import cn.chenforcode.entity.Student;
import cn.chenforcode.service.LoginService;
import cn.chenforcode.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    private HttpSession session;

    @Autowired
    private LoginService loginService;

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/loginPrompt")
    public String loginPrompt() {
        return "login";
    }

    @RequestMapping(value = "/doLogin")
    public String doLogin(String uname, String pwd) {
        String retValue = loginService.doLogin(uname, pwd);
        if ("admin_success".equals(retValue)) {
            //清楚错误信息
            if (session.getAttribute("login_msg") != null) {
                session.removeAttribute("login_msg");
            }
            session.setAttribute("uname", uname);
            session.setAttribute("pwd", pwd);
            return "redirect:/admin/admin_main";
        } else if ("stuadmin_success".equals(retValue)) {
            //清楚错误信息
            if (session.getAttribute("login_msg") != null) {
                session.removeAttribute("login_msg");
            }
            session.setAttribute("uname", uname);
            session.setAttribute("pwd", pwd);
            return "redirect:/admin/sadmin_main";
        } else if ("bookadmin_success".equals(retValue)) {
            //清楚错误信息
            if (session.getAttribute("login_msg") != null) {
                session.removeAttribute("login_msg");
            }
            session.setAttribute("uname", uname);
            session.setAttribute("pwd", pwd);
            return "redirect:/admin/badmin_main";
        } else if ("student_success".equals(retValue)) {
            //清楚错误信息
            if (session.getAttribute("login_msg") != null) {
                session.removeAttribute("login_msg");
            }
            session.setAttribute("uname", uname);
            session.setAttribute("pwd", pwd);
            return "redirect:/student/stu_main";
        } else {
            session.setAttribute("login_msg", "用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/doLogout")
    public String doLogout() {
        session.invalidate();
        return "redirect:loginPrompt";
    }

    @RequestMapping(value = "/regPro")
    public String regPro() {
        return "register";
    }

    @RequestMapping(value = "/doRegister")
    public String doRegister(Student student) {
        String msg = studentService.addStudent(student);

        if ("success".equals(msg)) {
            if (session.getAttribute("reg_msg") != null) {
                session.removeAttribute("reg_msg");
            }
            studentService.addStudent(student);
            return "redirect:/login/loginPrompt";
        } else {
            session.setAttribute("reg_msg", "学号已存在");
            return "redirect:/login/regPro";
        }
    }
}
